'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const path_1 = require('path');
const typescript_1 = require('typescript');
const defaultOptions = {
  components: [{ name: 'WContainer', props: 'title', value: '' }],
};

const createTransformerVueJsxProps = (_options = defaultOptions) => {
  const mergeDefault = (options) => Object.assign({}, defaultOptions, options);
  const componentMap = mergeDefault(_options);

  const createTransformerVueJsxProps = (context) => {
    let fileName;
    let componentMapCount = {};
    let componentName = '';

    const visitor = (node) => {
      if (typescript_1.isSourceFile(node)) {
        /* fileName = path_1.basename(node.fileName); */
        if (path_1.isAbsolute(node.fileName)) {
          fileName = node.fileName.replace(path_1.join(process.cwd(), '../'), '');
        } else {
          fileName = node.fileName;
        }
        return typescript_1.visitEachChild(node, visitor, context);
      }
      if (typescript_1.isClassDeclaration(node)) {
        componentName = node.name.getText();
        return typescript_1.visitEachChild(node, visitor, context);
      }
      /* if (typescript_1.isVariableStatement(node)) {
                componentName = node.getText();
                return typescript_1.visitEachChild(node, visitor, context);
            } */
      if (typescript_1.isJsxOpeningElement(node) && Array.isArray(componentMap.components)) {
        console.log('fileName', node.tagName.getText(), componentMap.components);
        componentMap.components.forEach(function(item) {
          if (
            item.props &&
            item.name &&
            node.tagName &&
            node.tagName.getText() === item.name &&
            (!item.value || typeof item.value === 'string' || typeof item.value === 'number')
          ) {
            if (componentMapCount[item.name]) {
              componentMapCount[item.name] += 1;
            } else {
              componentMapCount[item.name] = 1;
            }
            let value = null;
            if (!item.value) {
              value = typescript_1.createStringLiteral(
                fileName + '/' + componentName + '/' + item.name + 'uid' + componentMapCount[item.name]
              );
            } else if (typeof item.value === 'string') {
              value = typescript_1.createStringLiteral(item.value);
            } else if (typeof item.value === 'number') {
              value = typescript_1.createNumericLiteral(item.value);
            }
            const sourceJsxAttr = typescript_1.createJsxAttribute(
              typescript_1.createIdentifier(item.props),
              value
                ? value
                : typescript_1.createStringLiteral(
                    fileName + '/' + componentName + '/' + item.name + 'uid' + componentMapCount[item.name]
                  )
            );

            const jsxAttributes = typescript_1.createJsxAttributes([...node.attributes.properties, sourceJsxAttr]);

            node = typescript_1.updateJsxOpeningElement(node, node.tagName, node.typeArguments, jsxAttributes);
            return node;
          }
        });
      }
      if (
        typescript_1.isJsxSelfClosingElement(node) &&
        componentMap.components &&
        Array.isArray(componentMap.components)
      ) {
        componentMap.components.forEach(function(item) {
          if (
            item.props &&
            item.name &&
            node.tagName &&
            node.tagName.getText() === item.name &&
            (!item.value || typeof item.value === 'string' || typeof item.value === 'number')
          ) {
            if (componentMapCount[item.name]) {
              componentMapCount[item.name] += 1;
            } else {
              componentMapCount[item.name] = 1;
            }
            let value = null;
            if (!item.value) {
              value = typescript_1.createStringLiteral(
                fileName + '/' + componentName + '/' + item.name + 'uid' + componentMapCount[item.name]
              );
            } else if (typeof item.value === 'string') {
              value = typescript_1.createStringLiteral(item.value);
            } else if (typeof item.value === 'number') {
              value = typescript_1.createNumericLiteral(item.value);
            }
            const sourceJsxAttr = typescript_1.createJsxAttribute(
              typescript_1.createIdentifier(item.props),
              value
                ? value
                : typescript_1.createStringLiteral(
                    fileName + '/' + componentName + '/' + item.name + 'uid' + componentMapCount[item.name]
                  )
            );
            const jsxAttributes = typescript_1.createJsxAttributes([...node.attributes.properties, sourceJsxAttr]);
            node = typescript_1.updateJsxSelfClosingElement(node, node.tagName, node.typeArguments, jsxAttributes);
            return node;
          }
        });
      }
      return typescript_1.visitEachChild(node, visitor, context);
    };
    return (node) => typescript_1.visitNode(node, visitor);
  };
  return createTransformerVueJsxProps;
};
module.exports = {
  createTransformerVueJsxProps,
};
